﻿<?php
//session_start();

require_once "db.class.php";
require_once "server.class.php";
require_once "config.php";

$params = array("host"=>"$host", "user"=>"$user", "pass"=>"$pass", "db"=>"$db");
$db = new Dbclass($params, false);
$server = new ServerHelperClass($db, $tables);

$action = $_POST['action'];
$userId = $_POST['userId'];
$timeStamp = time();
$xmlContent = "";

//MD5验证，是否B1客户端发起
if ($server->generate_sig($_POST, $secret) != $_POST['b1_sig'])
{
	//$debug2 = $server->generate_sig($_POST, $secret)." <-> ".$_POST['b1_sig'];
	die();
}
if ($action == 'syncTime')
{
	$return = 'success';
}
else 
{
	if (!$userId)
	{
		$userId = $server->getUserId($_POST['renrenId']);
		if (!$userId)
		{
			$userId = $server->createUser($_POST['renrenId']);
		}
	}

	$sql = "update ".$tb_user_profile." set callId = ".$_POST['callId']." where id = ".$userId." and (callId is NULL or callId < ".$_POST['callId'].")";
	$result = $db->query($sql);
	if ($db->affectedRows() <= 0)
	{
		//$debug = $sql;
		//die($debug." + Call Id not up to date!");
	}
	//处理session，检查是否有两个重复登录
	if ($action == 'getSessionId')
	{
		$sql = "update ".$tb_user_profile." set sessionId = ".$timeStamp." where id = ".$userId;
		$result = $db->query($sql);
		if ($result)
		{
			$sessionId = $timeStamp;
			$return = "success";
		}
		else
		{
			$return = "false";
		}
	}
	else
	{
		$sql = "select * from ".$tb_user_profile." where id = ".$userId." and sessionId = ".$_POST['sessionId'];
		$result = $db->query($sql);
		if (!($arr = $db->fetchArray($result)))
		{
			$return = "other login";
		}
	}
}

if ($return != "other login")
{
if ($action == 'getSessionId')
{
	$xmlContent .= "<userId><![CDATA[".$userId."]]></userId>\n";
	$xmlContent .= "<sessionId><![CDATA[".$sessionId."]]></sessionId>\n";
	$return = "success";
}
else if ($action == "addUserOwnAnimal")
{
	$userName = $_POST['userName'];
	$animalIndex = $_POST['animalIndex'];
	$animalName = $_POST['animalName'];
	$homeIndex = $_POST['homeIndex'];
	$experience = $_POST['experience'];
	$level = $_POST['level'];
	$healthPoint = $_POST['healthPoint'];
	$vitalityPoint = $_POST['vitalityPoint'];
	$intelligence = $_POST['intelligence'];
	$speed = $_POST['speed'];
	$skillPoint = $_POST['skillPoint'];
	$sql = "update ".$tb_user_profile." set name = '".$userName."', homeIndex = ".$homeIndex." where id = ".$userId;
	$result = $db->query($sql);
	$animalId = $server->addAnimal($userId, $animalIndex, $animalName, $experience, $level, $healthPoint, $vitalityPoint, $intelligence, $speed, $skillPoint);
	if ($animalId > 0)
	{
		$splitAnimalClues = split(',', $_POST['animalClues']);
		$sql = "insert into ".$tb_animal_own_clues." (animalId, clueIndex, clueValue, fakeClueValue) values (".$animalId.", ".$splitAnimalClues[0].", ".$splitAnimalClues[1].", ".$splitAnimalClues[1].")";
		for ($i = 1; $i < (count($splitAnimalClues) - 1) / 2; $i++)
		{
			$sql .= ", (".$animalId.", ".$splitAnimalClues[2 * $i].", ".$splitAnimalClues[2 * $i + 1].", ".$splitAnimalClues[2 * $i + 1].")";
		}
		$result = $db->query($sql);
		$xmlContent .= "<animal>\n";
		$xmlContent .= "<userId><![CDATA[".$userId."]]></userId>\n";
		$xmlContent .= "<animalId><![CDATA[".$animalId."]]></animalId>\n";
		$xmlContent .= "<animalName><![CDATA[".$animalName."]]></animalName>\n";
		$xmlContent .= "<animalIndex><![CDATA[".$animalIndex."]]></animalIndex>\n";
		$xmlContent .= "<experience><![CDATA[".$experience."]]></experience>\n";
		$xmlContent .= "<level><![CDATA[".$level."]]></level>\n";
		$xmlContent .= "<healthPointMax><![CDATA[".$healthPointMax."]]></healthPointMax>\n";
		$xmlContent .= "<healthPoint><![CDATA[".$healthPoint."]]></healthPoint>\n";
		$xmlContent .= "<vitalityPointMax><![CDATA[".$vitalityPointMax."]]></vitalityPointMax>\n";
		$xmlContent .= "<vitalityPoint><![CDATA[".$vitalityPoint."]]></vitalityPoint>\n";
		$xmlContent .= "<intelligence><![CDATA[".$intelligence."]]></intelligence>\n";
		$xmlContent .= "<speed><![CDATA[".$speed."]]></speed>\n";
		$xmlContent .= "<skillPoint><![CDATA[".$skillPoint."]]></skillPoint>\n";
		$xmlContent .= "<ownerId><![CDATA[".$userId."]]></ownerId>\n";
		$xmlContent .= "<liveId><![CDATA[".$userId."]]></liveId>\n";
		$xmlContent .= "</animal>\n";
		$return = "success";
	}
	else
	{
		$return = "fail";
	}
}
else if ($action == "getTopAnimals")
{ 
	$num = $_POST['num'];
	$sql = "select playerId, sum(equipLevel) as equipScore from ".$tb_player_honour." group by playerId order by equipScore desc limit 0, ".$num;
	$result = $db->query($sql);
	$count = 0;
	while ($arr = $db->fetchArray($result))
	{
		$playerId = $arr['playerId'];
		if ($count == 0)
			$sql = "select playerId, equipId, renrenId, equipLevel, heirloomLevelCount, heirloomLevelRank from ".$tb_player_honour." a inner join ".$tb_user_profile." b on a.playerId = b.id where playerId = ".$playerId;
		else
			$sql .= " or playerId = ".$playerId;
		$count++;
	}
	$result = $db->query($sql);
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .="<player>\n";
		$xmlContent .= "<playerId><![CDATA[".$arr['playerId']."]]></playerId>\n";
		$xmlContent .= "<equipId><![CDATA[".$arr['equipId']."]]></equipId>\n";
		$xmlContent .= "<renrenId><![CDATA[".$arr['renrenId']."]]></renrenId>\n";
		$xmlContent .= "<equipLevel><![CDATA[".$arr['equipLevel']."]]></equipLevel>\n";
		$xmlContent .= "<heirloomLevelCount><![CDATA[".$arr['heirloomLevelCount']."]]></heirloomLevelCount>\n";
		$xmlContent .= "<heirloomLevelRank><![CDATA[".$arr['heirloomLevelRank']."]]></heirloomLevelRank>\n";
		$xmlContent .= "</player>\n";
	}
	$return = "success";
}
else if ($action == "getRrFriends")
{
	$splitRrId = split(',', $_POST['rrPlayerIds']);
	$sql = "select a.id as playerId, a.renrenId as ownerRrId, a.name as playerName, b.id as animalId, b.name as animalName, animalIndex, experience, level, healthPointMax, healthPoint, vitalityPointMax, vitalityPoint, intelligence, speed, skillPoint, ownerId, liveId, timeToGetFree, hpSaveTime from ".$tb_user_profile." a left join ".$tb_animal_profile." b on a.id = b.liveId where a.renrenId = ".$splitRrId[0];
	for ($i = 1; $i < count($splitRrId); $i++)
	{
		$sql .= " or a.renrenId = ".$splitRrId[$i];
	}
	$result = $db->query($sql);
	$sql2 = "select animalId, position, equipId, objectIndex, durability from ".$tb_animal_ware_equip." a inner join ".$tb_equip_profile." b on a.equipId = b.id where";
	$friendCount = 0;
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<liveAnimal>\n";
		$xmlContent .= "<animalId><![CDATA[".$arr['animalId']."]]></animalId>\n";
		$xmlContent .= "<animalName><![CDATA[".$arr['animalName']."]]></animalName>\n";
		$xmlContent .= "<ownerName><![CDATA[".$arr['playerName']."]]></ownerName>\n";
		$xmlContent .= "<ownerRrId><![CDATA[".$arr['ownerRrId']."]]></ownerRrId>\n";
		$xmlContent .= "<animalIndex><![CDATA[".$arr['animalIndex']."]]></animalIndex>\n";
		$xmlContent .= "<experience><![CDATA[".$arr['experience']."]]></experience>\n";
		$xmlContent .= "<level><![CDATA[".$arr['level']."]]></level>\n";
		$xmlContent .= "<healthPointMax><![CDATA[".$arr['healthPointMax']."]]></healthPointMax>\n";
		$xmlContent .= "<healthPoint><![CDATA[".$arr['healthPoint']."]]></healthPoint>\n";
		$xmlContent .= "<vitalityPointMax><![CDATA[".$arr['vitalityPointMax']."]]></vitalityPointMax>\n";
		$xmlContent .= "<vitalityPoint><![CDATA[".$arr['vitalityPoint']."]]></vitalityPoint>\n";
		$xmlContent .= "<intelligence><![CDATA[".$arr['intelligence']."]]></intelligence>\n";
		$xmlContent .= "<speed><![CDATA[".$arr['speed']."]]></speed>\n";
		$xmlContent .= "<skillPoint><![CDATA[".$arr['skillPoint']."]]></skillPoint>\n";
		$xmlContent .= "<ownerId><![CDATA[".$arr['ownerId']."]]></ownerId>\n";
		$xmlContent .= "<liveId><![CDATA[".$arr['playerId']."]]></liveId>\n";
		$xmlContent .= "<timeToGetFree><![CDATA[".$arr['timeToGetFree']."]]></timeToGetFree>\n";
		$xmlContent .= "<hpSaveTime><![CDATA[".$arr['hpSaveTime']."]]></hpSaveTime>\n";
		$xmlContent .= "</liveAnimal>\n";
		if ($arr['animalId'] != "")
		{
			$friendCount++;
			if ($friendCount == 1)
				$sql2 .=  " a.animalId = ".$arr['animalId'];
			else
				$sql2 .=  " or a.animalId = ".$arr['animalId'];
		}
	}
	//获得装备信息
	$result2 = $db->query($sql2);
	while ($arr2 = $db->fetchArray($result2))
	{
		$xmlContent .= "<equip>\n";
		$xmlContent .= "<animalId><![CDATA[".$arr2['animalId']."]]></animalId>\n";
		$xmlContent .= "<equipId><![CDATA[".$arr2['equipId']."]]></equipId>\n";
		$xmlContent .= "<position><![CDATA[".$arr2['position']."]]></position>\n";
		$xmlContent .= "<objectIndex><![CDATA[".$arr2['objectIndex']."]]></objectIndex>\n";
		$xmlContent .= "<durability><![CDATA[".$arr2['durability']."]]></durability>\n";
		$xmlContent .= "</equip>\n";
	}
	$return = "success";
}
else if ($action == "getMeAndNeighbors")
{
	$sql = "select a.id as playerId, a.renrenId as ownerRrId, a.name as playerName, a.homeIndex as homeIndex, b.id as animalId, b.name as animalName, animalIndex, experience, level, healthPointMax, healthPoint, vitalityPointMax, vitalityPoint, intelligence, speed, skillPoint, ownerId, liveId, timeToGetFree, hpSaveTime from ".$tb_user_profile." a left join ".$tb_animal_profile." b on a.id = b.ownerId where a.id = ".$userId;
	$result = $db->query($sql);
	if ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<ownAnimal>\n";
		$xmlContent .= "<animalId><![CDATA[".$arr['animalId']."]]></animalId>\n";
		$xmlContent .= "<animalName><![CDATA[".$arr['animalName']."]]></animalName>\n";
		$xmlContent .= "<ownerName><![CDATA[".$arr['playerName']."]]></ownerName>\n";
		$xmlContent .= "<ownerRrId><![CDATA[".$arr['ownerRrId']."]]></ownerRrId>\n";
		$xmlContent .= "<homeIndex><![CDATA[".$arr['homeIndex']."]]></homeIndex>\n";
		$xmlContent .= "<animalIndex><![CDATA[".$arr['animalIndex']."]]></animalIndex>\n";
		$xmlContent .= "<experience><![CDATA[".$arr['experience']."]]></experience>\n";
		$xmlContent .= "<level><![CDATA[".$arr['level']."]]></level>\n";
		$xmlContent .= "<healthPointMax><![CDATA[".$arr['healthPointMax']."]]></healthPointMax>\n";
		$xmlContent .= "<healthPoint><![CDATA[".$arr['healthPoint']."]]></healthPoint>\n";
		$xmlContent .= "<vitalityPointMax><![CDATA[".$arr['vitalityPointMax']."]]></vitalityPointMax>\n";
		$xmlContent .= "<vitalityPoint><![CDATA[".$arr['vitalityPoint']."]]></vitalityPoint>\n";
		$xmlContent .= "<intelligence><![CDATA[".$arr['intelligence']."]]></intelligence>\n";
		$xmlContent .= "<speed><![CDATA[".$arr['speed']."]]></speed>\n";
		$xmlContent .= "<skillPoint><![CDATA[".$arr['skillPoint']."]]></skillPoint>\n";
		$xmlContent .= "<ownerId><![CDATA[".$arr['playerId']."]]></ownerId>\n";
		$xmlContent .= "<liveId><![CDATA[".$arr['liveId']."]]></liveId>\n";
		$xmlContent .= "<timeToGetFree><![CDATA[".$arr['timeToGetFree']."]]></timeToGetFree>\n";
		$xmlContent .= "<hpSaveTime><![CDATA[".$arr['hpSaveTime']."]]></hpSaveTime>\n";
		$xmlContent .= "</ownAnimal>\n";
		$sql2 = "select animalId, position, equipId, objectIndex, durability from ".$tb_animal_ware_equip." a inner join ".$tb_equip_profile." b on a.equipId = b.id where a.animalId = ".$arr['animalId'];
	}
	$neighborNum = $_POST['neighborNum'];
	$leftNeigborBound = $userId - $neighborNum / 2;
	$rightNeigborBound = $userId + $neighborNum / 2;
	$sql = "select a.id as playerId, a.renrenId as ownerRrId, a.name as playerName, a.homeIndex as homeIndex, b.id as animalId, b.name as animalName, animalIndex, experience, level, healthPointMax, healthPoint, vitalityPointMax, vitalityPoint, intelligence, speed, skillPoint, ownerId, liveId, timeToGetFree, hpSaveTime from ".$tb_user_profile." a left join ".$tb_animal_profile." b on a.id = b.liveId where a.id >= ".$leftNeigborBound." and a.id <= ".$rightNeigborBound;
	$result = $db->query($sql);
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<liveAnimal>\n";
		$xmlContent .= "<animalId><![CDATA[".$arr['animalId']."]]></animalId>\n";
		$xmlContent .= "<animalName><![CDATA[".$arr['animalName']."]]></animalName>\n";
		$xmlContent .= "<ownerName><![CDATA[".$arr['playerName']."]]></ownerName>\n";
		$xmlContent .= "<ownerRrId><![CDATA[".$arr['ownerRrId']."]]></ownerRrId>\n";
		$xmlContent .= "<homeIndex><![CDATA[".$arr['homeIndex']."]]></homeIndex>\n";
		$xmlContent .= "<animalIndex><![CDATA[".$arr['animalIndex']."]]></animalIndex>\n";
		$xmlContent .= "<experience><![CDATA[".$arr['experience']."]]></experience>\n";
		$xmlContent .= "<level><![CDATA[".$arr['level']."]]></level>\n";
		$xmlContent .= "<healthPointMax><![CDATA[".$arr['healthPointMax']."]]></healthPointMax>\n";
		$xmlContent .= "<healthPoint><![CDATA[".$arr['healthPoint']."]]></healthPoint>\n";
		$xmlContent .= "<vitalityPointMax><![CDATA[".$arr['vitalityPointMax']."]]></vitalityPointMax>\n";
		$xmlContent .= "<vitalityPoint><![CDATA[".$arr['vitalityPoint']."]]></vitalityPoint>\n";
		$xmlContent .= "<intelligence><![CDATA[".$arr['intelligence']."]]></intelligence>\n";
		$xmlContent .= "<speed><![CDATA[".$arr['speed']."]]></speed>\n";
		$xmlContent .= "<skillPoint><![CDATA[".$arr['skillPoint']."]]></skillPoint>\n";
		$xmlContent .= "<ownerId><![CDATA[".$arr['ownerId']."]]></ownerId>\n";
		$xmlContent .= "<liveId><![CDATA[".$arr['playerId']."]]></liveId>\n";
		$xmlContent .= "<timeToGetFree><![CDATA[".$arr['timeToGetFree']."]]></timeToGetFree>\n";
		$xmlContent .= "<hpSaveTime><![CDATA[".$arr['hpSaveTime']."]]></hpSaveTime>\n";
		$xmlContent .= "</liveAnimal>\n";
		if ($arr['animalId'] != "")
			$sql2 .=  " or a.animalId = ".$arr['animalId'];
	}
	//获得装备信息
	$result2 = $db->query($sql2);
	while ($arr2 = $db->fetchArray($result2))
	{
		$xmlContent .= "<equip>\n";
		$xmlContent .= "<animalId><![CDATA[".$arr2['animalId']."]]></animalId>\n";
		$xmlContent .= "<equipId><![CDATA[".$arr2['equipId']."]]></equipId>\n";
		$xmlContent .= "<position><![CDATA[".$arr2['position']."]]></position>\n";
		$xmlContent .= "<objectIndex><![CDATA[".$arr2['objectIndex']."]]></objectIndex>\n";
		$xmlContent .= "<durability><![CDATA[".$arr2['durability']."]]></durability>\n";
		$xmlContent .= "</equip>\n";
	}
	//获得玩家本人信息
	$sql = "select renrenId, name, homeIndex, accomplishedTaskId, accomplishedFestivalTaskId, heirloomBoxBoneLeft, heirloomBoxFetchTime, heirloomLevelCount, heirloomLevelRank from ".$tb_user_profile." where id = ".$userId;
	$result = $db->query($sql);
	$xmlContent .= "<userInfo>\n";
	if ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<userId><![CDATA[".$userId."]]></userId>\n";
		$xmlContent .= "<renrenId><![CDATA[".$arr['renrenId']."]]></renrenId>\n";
		$xmlContent .= "<userName><![CDATA[".$arr['name']."]]></userName>\n";
		$xmlContent .= "<homeIndex><![CDATA[".$arr['homeIndex']."]]></homeIndex>\n";
		$xmlContent .= "<accomplishedTaskId><![CDATA[".$arr['accomplishedTaskId']."]]></accomplishedTaskId>\n";
		$xmlContent .= "<accomplishedFestivalTaskId><![CDATA[".$arr['accomplishedFestivalTaskId']."]]></accomplishedFestivalTaskId>\n";
		$xmlContent .= "<heirloomBoxBoneLeft><![CDATA[".$arr['heirloomBoxBoneLeft']."]]></heirloomBoxBoneLeft>\n";
		$xmlContent .= "<heirloomBoxFetchTime><![CDATA[".$arr['heirloomBoxFetchTime']."]]></heirloomBoxFetchTime>\n";
		$xmlContent .= "<heirloomLevelCount><![CDATA[".$arr['heirloomLevelCount']."]]></heirloomLevelCount>\n";
		$xmlContent .= "<heirloomLevelRank><![CDATA[".$arr['heirloomLevelRank']."]]></heirloomLevelRank>\n";
		$sql = "select equipId, equipLevel from ".$tb_player_honour." where playerId = ".$userId;
		$res = $db->query($sql);
		while ($arr3 = $db->fetchArray($res))
		{
			$xmlContent .= "<honourInfo>";
			$xmlContent .= "<equipsId><![CDATA[".$arr3['equipId']."]]></equipsId>\n";
			$xmlContent .= "<equipsLevel><![CDATA[".$arr3['equipLevel']."]]></equipsLevel>\n";
			$xmlContent .= "</honourInfo>";
		}
	}
	$xmlContent .= "</userInfo>\n";
	//获得物品清单
	$sql = "select objectIndex, objectNum from ".$tb_user_own_object." where userId = ".$userId;
	$result = $db->query($sql);
	$xmlContent .= "<packageInfo>\n";
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<object>\n";
		$xmlContent .= "<userId><![CDATA[".$userId."]]></userId>\n";
		$xmlContent .= "<objectIndex><![CDATA[".$arr['objectIndex']."]]></objectIndex>\n";
		$xmlContent .= "<objectNum><![CDATA[".$arr['objectNum']."]]></objectNum>\n";
		$xmlContent .= "</object>\n";
	}
	$xmlContent .= "</packageInfo>\n";
	$return = "success";
}
else if ($action == "updatePlayerHonour")
{
	$userId = $_POST['userId'];
	$equipsId = $_POST['equipsId'];
	$equipsLevel = $_POST['equipsLevel'];
	$sql = "update ".$tb_player_honour." set equipLevel = ".$equipsLevel." where playerId = ".$userId." and equipId = ".$equipsId." and equipLevel < ".$equipsLevel;
	$db->query($sql);
	if ($db->affectedRows() <= 0)
	{
		$sql = "insert into ".$tb_player_honour." (playerId,equipId,equipLevel) value (".$userId.",".$equipsId.",".$equipsLevel.")";
		$db->query($sql);
	}
}
else if ($action == "setCaseDetected")
{
	$caseId = $_POST['caseId'];
	$ownerDetectedFlag = $_POST['ownerDetectedFlag'];
	$otherDetectedFlag = $_POST['otherDetectedFlag'];
	$sql = "update ".$tb_battle_case." set ownerDetectedFlag = ".$ownerDetectedFlag.", otherDetectedFlag = ".$otherDetectedFlag." where id = ".$caseId;
	$db->query($sql);
	if ($db->affectedRows() <= 0)
	{
		$return = "false";
	}
	else
	{
		$return = "success";
	}
}
else if ($action == "getAllCases")
{
	$splitAnimalId = split(',', $_POST['animalIds']);
	$detectiveAnimalId = $_POST['detectiveAnimalId'];
	$sql = "select id, winnerAnimalId, loserAnimalId, caseBones, ownerDetectedFlag, otherDetectedFlag, clueIndex, clueValue, timeToDisappear, loseHeirloomId from ".$tb_battle_case." a inner join ".$tb_case_own_clues." b on a.id = b.caseId where winnerAnimalId != ".$detectiveAnimalId." and  (loserAnimalId = ".$splitAnimalId[0];
	for ($i = 1; $i < count($splitAnimalId) - 1; $i++)
	{
		$sql .= " or loserAnimalId = ".$splitAnimalId[$i];
	}
	$sql .= ") and (loserAnimalId = ".$detectiveAnimalId." and ownerDetectedFlag = false or loserAnimalId != ".$detectiveAnimalId." and otherDetectedFlag = false) and timeToDisappear > ".$timeStamp." limit 0,64";
	$result = $db->query($sql);
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<clue>\n";
		$xmlContent .= "<caseId><![CDATA[".$arr['id']."]]></caseId>\n";
		$xmlContent .= "<loserAnimalId><![CDATA[".$arr['loserAnimalId']."]]></loserAnimalId>\n";
		$xmlContent .= "<caseBones><![CDATA[".$arr['caseBones']."]]></caseBones>\n";
		$xmlContent .= "<ownerDetectedFlag><![CDATA[".$arr['ownerDetectedFlag']."]]></ownerDetectedFlag>\n";
		$xmlContent .= "<otherDetectedFlag><![CDATA[".$arr['otherDetectedFlag']."]]></otherDetectedFlag>\n";
		$xmlContent .= "<clueIndex><![CDATA[".$arr['clueIndex']."]]></clueIndex>\n";
		$xmlContent .= "<clueValue><![CDATA[".$arr['clueValue']."]]></clueValue>\n";
		$xmlContent .= "<timeToDisappear><![CDATA[".$arr['timeToDisappear']."]]></timeToDisappear>\n";
		$xmlContent .= "<loseHeirloomId><![CDATA[".$arr['loseHeirloomId']."]]></loseHeirloomId>\n";
		$xmlContent .= "</clue>\n";
	}
	$return = "success";
}
else if ($action == "getMyCases")
{
	$animalId = $_POST['animalId'];
	$sql = "select a.id as caseId, winnerAnimalId, loserAnimalId, caseBones, ownerDetectedFlag, otherDetectedFlag, clueIndex, clueValue, timeToDisappear, loseHeirloomId, detectiveOwnerId, c.name as detectiveOwnerName, e.name as loserOwnerName from ".$tb_battle_case." a inner join ".$tb_case_own_clues." b on a.id = b.caseId left join ".$tb_user_profile." c on a.detectiveOwnerId = c.id inner join ".$tb_animal_profile." d on a.loserAnimalId = d.id inner join ".$tb_user_profile." e on d.ownerId = e.id where winnerAnimalId = ".$animalId." and timeToDisappear > ".$timeStamp." - 86400 limit 0,64";;
	$debug = $sql;
	$result = $db->query($sql);
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<clue>\n";
		$xmlContent .= "<caseId><![CDATA[".$arr['caseId']."]]></caseId>\n";
		$xmlContent .= "<loserAnimalId><![CDATA[".$arr['loserAnimalId']."]]></loserAnimalId>\n";
		$xmlContent .= "<caseBones><![CDATA[".$arr['caseBones']."]]></caseBones>\n";
		$xmlContent .= "<ownerDetectedFlag><![CDATA[".$arr['ownerDetectedFlag']."]]></ownerDetectedFlag>\n";
		$xmlContent .= "<otherDetectedFlag><![CDATA[".$arr['otherDetectedFlag']."]]></otherDetectedFlag>\n";
		$xmlContent .= "<clueIndex><![CDATA[".$arr['clueIndex']."]]></clueIndex>\n";
		$xmlContent .= "<clueValue><![CDATA[".$arr['clueValue']."]]></clueValue>\n";
		$xmlContent .= "<timeToDisappear><![CDATA[".$arr['timeToDisappear']."]]></timeToDisappear>\n";
		$xmlContent .= "<loseHeirloomId><![CDATA[".$arr['loseHeirloomId']."]]></loseHeirloomId>\n";
		$xmlContent .= "<detectiveOwnerId><![CDATA[".$arr['detectiveOwnerId']."]]></detectiveOwnerId>\n";
		$xmlContent .= "<detectiveOwnerName><![CDATA[".$arr['detectiveOwnerName']."]]></detectiveOwnerName>\n";
		$xmlContent .= "<loserOwnerName><![CDATA[".$arr['loserOwnerName']."]]></loserOwnerName>\n";
		$xmlContent .= "</clue>\n";
	}
	$return = "success";
}
else if ($action == "addCase")
{
	$winnerAnimalId = $_POST['winnerAnimalId'];
	$loserAnimalId = $_POST['loserAnimalId'];
	$caseBones = $_POST['caseBones'];
	$sql = "insert into ".$tb_battle_case." (winnerAnimalId, loserAnimalId, caseBones) VALUES (".$winnerAnimalId.", ".$loserAnimalId.", ".$caseBones.")";
	$result = $db->query($sql);
	$sql = "select last_insert_id()";
	$result = $db->query($sql);
	$arr = $db->fetchArray($result);
	$caseId = $arr['last_insert_id()'];
	$clues = split(',', $_POST['clueInfo']);
	$sql = "insert into ".$tb_case_own_clues." (caseId, clueIndex, clueValue, timeToDisappear) VALUES (".$caseId.", ".$clues[0].", ".$clues[1].", ".$clues[2].")";	
	$xmlContent .= "<clue>\n";
	$xmlContent .= "<caseId><![CDATA[".$caseId."]]></caseId>\n";
	$xmlContent .= "<clueIndex><![CDATA[".$clues[0]."]]></clueIndex>\n";
	$xmlContent .= "<clueValue><![CDATA[".$clues[1]."]]></clueValue>\n";
	$xmlContent .= "<timeToDisappear><![CDATA[".$clues[2]."]]></timeToDisappear>\n";
	$xmlContent .= "</clue>\n";
	for ($i = 1; $i < (count($clues) - 1) / 3; $i++)
	{
		$sql .= ", (".$caseId.", ".$clues[3 * $i].", ".$clues[3 * $i + 1].", ".$clues[3 * $i + 2].")";
		$xmlContent .= "<clue>\n";
		$xmlContent .= "<caseId><![CDATA[".$caseId."]]></caseId>\n";
		$xmlContent .= "<clueIndex><![CDATA[".$clues[3 * $i]."]]></clueIndex>\n";
		$xmlContent .= "<clueValue><![CDATA[".$clues[3 * $i + 1]."]]></clueValue>\n";
		$xmlContent .= "<timeToDisappear><![CDATA[".$clues[3 * $i + 2]."]]></timeToDisappear>\n";
		$xmlContent .= "</clue>\n";
	}
	$result = $db->query($sql);
	$loseHeirloomId = $_POST['loseHeirloomId'];
	$sql = "update ".$tb_battle_case." set loseHeirloomId = ".$loseHeirloomId." where id = ".$caseId;
	$xmlContent .= "<caseId><![CDATA[".$caseId."]]></caseId>\n";
	$xmlContent .= "<caseBones><![CDATA[".$caseBones."]]></caseBones>\n";
	$xmlContent .= "<loseHeirloomId><![CDATA[".$loseHeirloomId."]]></loseHeirloomId>\n";
	$result = $db->query($sql);
	$return = "success";
}
else if ($action == "getRandomSuspect")
{
	//取随机动物
	if (md5($_POST['t'].$randomStr) == $_POST['magicKey'])
	{
		$sql = "select count(*) as animal_num from ".$tb_animal_profile;
		$result = $db->query($sql);
		$arr = $db->fetchArray($result);
		$animal_num = $arr['animal_num'];
		$randomIndex = rand(1, $animal_num);
		$xmlContent .= $server->getAnimalXML($randomIndex);
	}
	//取真正的凶手
	else
	{
		$caseId = $_POST['caseId'];
		$sql = "select winnerAnimalId from ".$tb_battle_case." where id = ".$caseId;
		$result = $db->query($sql);
		$arr = $db->fetchArray($result);
		$animalId = $arr['winnerAnimalId'];
		$xmlContent .= $server->getAnimalXML($animalId);
	}
	$return = "success";
}
else if ($action == 'sellObject')
{
	$objectIndex = $_POST['objectIndex'];
	$objectNum = $_POST['objectNum'];
	$getBones = $_POST['priceBones'] * $objectNum;
	$getGoldBones = $_POST['priceGoldBones'] * $objectNum;
	$debug = $sql;
	//
	$sql = "select objectNum from ".$tb_user_own_object." where userId = ".$userId." and objectIndex = ".$objectIndex;
	$result = $db->query($sql);
	$arr = $db ->fetchArray($result);
	$currentNum = $arr['objectNum'];
	if ($currentNum < $objectNum)
	{
		$return = "not enough object";
	}
	else
	{
		//sell
		$sql = "update ".$tb_user_own_object." set objectNum = objectNum - ".$objectNum." where userId = ".$userId." and objectIndex = ".$objectIndex;
		$db->query($sql);
		//pay
		$sql = "update ".$tb_user_own_object." set objectNum = objectNum + ".$getBones." where userId = ".$userId." and objectIndex = 0";
		$db->query($sql);
		$return = "success";
	}
	//获得物品清单
	$sql = "select objectIndex, objectNum from ".$tb_user_own_object." where userId = ".$userId;
	$result = $db->query($sql);
	$xmlContent .= "<packageInfo>\n";
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<object>\n";
		$xmlContent .= "<userId><![CDATA[".$userId."]]></userId>\n";
		$xmlContent .= "<objectIndex><![CDATA[".$arr['objectIndex']."]]></objectIndex>\n";
		$xmlContent .= "<objectNum><![CDATA[".$arr['objectNum']."]]></objectNum>\n";
		$xmlContent .= "</object>\n";
	}
	$xmlContent .= "</packageInfo>\n";
}
else if ($action == 'purchaseObject')
{
	$objectIndex = $_POST['objectIndex'];
	$objectNum = $_POST['objectNum'];
	$needBones = $_POST['priceBones'] * $objectNum;
	$needGoldBones = $_POST['priceGoldBones'] * $objectNum;
	$debug = $sql;

	$sql = "select objectNum from ".$tb_user_own_object." where userId = ".$userId." and objectIndex = 0";
	$result = $db->query($sql);
	$arr = $db->fetchArray($result);
	$currentBones = $arr['objectNum'];
	$sql = "select objectNum from ".$tb_user_own_object." where userId = ".$userId." and objectIndex = 1";
	$result = $db->query($sql);
	$arr = $db->fetchArray($result);
	$currentGoldBones = $arr['objectNum'];
	//检查是否有足够的骨头
	if ($needBones > $currentBones && $needBones > 0)
	{
		$return = "not enough bones";
	}
	else if ($needGoldBones > $currentGoldBones && $needGoldBones > 0)
	{
		$return = "not enough goldbones";
	}
	else
	{
		//购买
		$sql = "update ".$tb_user_own_object." set objectNum = objectNum + ".$objectNum." where userId = ".$userId." and objectIndex = ".$objectIndex;
		$db->query($sql);
		if ($db->affectedRows() <= 0)
		{
			$sql = "insert into ".$tb_user_own_object." (userId, objectIndex, objectNum) values (".$userId.", ".$objectIndex.", ".$objectNum.")";
			$db->query($sql);
		}
		//付账
		$sql = "update ".$tb_user_own_object." set objectNum = objectNum - ".$needBones." where userId = ".$userId." and objectIndex = 0";
		$db->query($sql);
		$sql = "update ".$tb_user_own_object." set objectNum = objectNum - ".$needGoldBones." where userId = ".$userId." and objectIndex = 1";
		$db->query($sql);
		$return = "success";
	}
	//获得物品清单
	$sql = "select objectIndex, objectNum from ".$tb_user_own_object." where userId = ".$userId;
	$result = $db->query($sql);
	$xmlContent .= "<packageInfo>\n";
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<object>\n";
		$xmlContent .= "<userId><![CDATA[".$userId."]]></userId>\n";
		$xmlContent .= "<objectIndex><![CDATA[".$arr['objectIndex']."]]></objectIndex>\n";
		$xmlContent .= "<objectNum><![CDATA[".$arr['objectNum']."]]></objectNum>\n";
		$xmlContent .= "</object>\n";
	}
	$xmlContent .= "</packageInfo>\n";
}
else if ($action == "getPackageInfo")
{
	//获得物品清单
	$splitUserId = split(',', $_POST['playerIds']);
	for ($i = 0; $i < count($splitUserId) - 1; $i++)
	{
		$sql = "select objectIndex, objectNum from ".$tb_user_own_object." where userId = ".$splitUserId[$i];
		$result = $db->query($sql);
		$xmlContent .= "<packageInfo>\n";
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<object>\n";
			$xmlContent .= "<userId><![CDATA[".$splitUserId[$i]."]]></userId>\n";
			$xmlContent .= "<objectIndex><![CDATA[".$arr['objectIndex']."]]></objectIndex>\n";
			$xmlContent .= "<objectNum><![CDATA[".$arr['objectNum']."]]></objectNum>\n";
			$xmlContent .= "</object>\n";
		}
		$xmlContent .= "</packageInfo>\n";
	}
	$return = "success";
}
//指认嫌疑犯
else if ($action == "identifySuspectAnimal")
{
	$caseId = $_POST['caseId'];
	$detectiveOwnerId = $_POST['detectiveOwnerId'];
	$suspectAnimalId = $_POST['suspectAnimalId'];
	$claimBones = $_POST['claimBones'];
	$sql = "select count(*) as c from ".$tb_battle_case." where id = ".$caseId." and winnerAnimalId = ".$suspectAnimalId;
	$result = $db->query($sql);
	if (($arr = $db->fetchArray($result)) && $arr['c'] > 0)
	{
		//正确指认，破案
		$sql = "update ".$tb_battle_case." set ownerDetectedFlag = true, otherDetectedFlag = true, detectiveOwnerId = ".$detectiveOwnerId." where id = ".$caseId;
		$db->query($sql);
		$return = "right";
	}
	else
	{
		//错误指认
		//付出赔偿金
		$sql = "update ".$tb_user_own_object." set objectNum = objectNum - ".$claimBones." where userId = ".$detectiveOwnerId." and objectIndex = 0";
		$db->query($sql);
		//获得物品清单
		$sql = "select objectIndex, objectNum from ".$tb_user_own_object." where userId = ".$userId;
		$result = $db->query($sql);
		$xmlContent .= "<packageInfo>\n";
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<object>\n";
			$xmlContent .= "<userId><![CDATA[".$userId."]]></userId>\n";
			$xmlContent .= "<objectIndex><![CDATA[".$arr['objectIndex']."]]></objectIndex>\n";
			$xmlContent .= "<objectNum><![CDATA[".$arr['objectNum']."]]></objectNum>\n";
			$xmlContent .= "</object>\n";
		}
		$xmlContent .= "</packageInfo>\n";
		$return = "wrong";
	}
}
//逮捕罪犯
else if ($action == "arrestAnimal")
{
	$detectiveAnimalId = $_POST['detectiveAnimalId'];
	$arrestTargetAnimalId = $_POST['arrestTargetAnimalId'];
	$timeToGetFree = $_POST['timeToGetFree'];
	$sql = "update ".$tb_animal_profile." set timeToGetFree = ".$timeToGetFree." where id = ".$arrestTargetAnimalId;
	$db->query($sql);
	$xmlContent .= "<arrestTargetAnimalId><![CDATA[".$arrestTargetAnimalId."]]></arrestTargetAnimalId>\n";
	$xmlContent .= "<timeToGetFree><![CDATA[".$timeToGetFree."]]></timeToGetFree>\n";
	if ($db->affectedRows() <= 0)
	{
		$return = "fail";
	}
	else
	{
		$return = "success";
	}
}
//找寻传家骨头所在地方
else if ($action == "findCrimerHome")
{
	$playerId = $_POST['playerId'];
	$sql = "select a.ownerId,a.liveId from ".$tb_animal_profile." a inner join ".$tb_heirloom." b on a.ownerId = b.currentOwnerPlayerId where b.originalOwnerPlayerId = ".$playerId;
	$result = $db->query($sql);
	$arr = $db->fetchArray($result);
	$liveId = $arr['liveId'];
	$ownerId = $arr['ownerId'];
	$xmlContent .= "<crimerOwnerId><![CDATA[".$ownerId."]]></crimerOwnerId>\n";
	$xmlContent .= "<crimerLiveId><![CDATA[".$liveId."]]></crimerLiveId>\n";
}
//找寻犯罪嫌疑人所在地
else if ($action == "findCrimerSite")
{
	$playerId = $_POST['playerId'];
	$sql = "select liveId from ".$tb_animal_profile." where ownerId = ".$playerId;
	$result = $db->query($sql);
	$arr = $db->fetchArray($result);
	$liveId = $arr['liveId'];
	$xmlContent .= "<crimerLiveIds><![CDATA[".$liveId."]]></crimerLiveIds>\n";
}
//保释动物
else if ($action == "bailAnimal")
{
	$animalId = $_POST['animalId'];
	$bailBones = $_POST['bailBones'];
	$sql = "select objectNum from ".$tb_user_own_object." where userId = ".$userId." and objectIndex = 0";
	$result = $db->query($sql);
	$arr = $db->fetchArray($result);
	$currentBones = $arr['objectNum'];
	//检查是否有足够的骨头
	if ($bailBones > $currentBones)
	{
		$return = "not enough bones";
	}
	else
	{
		$sql = "update ".$tb_animal_profile." set timeToGetFree = 0 where id = ".$animalId;
		$db->query($sql);
		$xmlContent .= "<animalId><![CDATA[".$animalId."]]></animalId>\n";
		if ($db->affectedRows() <= 0)
		{
			$return = "fail";
		}
		else
		{
			$return = "success";
			//付保释金
			$sql = "update ".$tb_user_own_object." set objectNum = objectNum - ".$bailBones." where userId = ".$userId." and objectIndex = 0";
			$db->query($sql);
		}
	}
	//获得物品清单
	$sql = "select objectIndex, objectNum from ".$tb_user_own_object." where userId = ".$userId;
	$result = $db->query($sql);
	$xmlContent .= "<packageInfo>\n";
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<object>\n";
		$xmlContent .= "<userId><![CDATA[".$userId."]]></userId>\n";
		$xmlContent .= "<objectIndex><![CDATA[".$arr['objectIndex']."]]></objectIndex>\n";
		$xmlContent .= "<objectNum><![CDATA[".$arr['objectNum']."]]></objectNum>\n";
		$xmlContent .= "</object>\n";
	}
	$xmlContent .= "</packageInfo>\n";
}
//发布通缉令
else if ($action == "postWantedAnimal")
{
	$caseId = $_POST['caseId'];
	$wantedBones = $_POST['wantedBones'];
	$timeToClose = $_POST['timeToClose'];
	$sql = "update ".$tb_battle_case." set wantedBones = ".$wantedBones.", timeToClose = ".$timeToClose." where id = ".$caseId;
	$db->query($sql);
	if ($db->affectedRows() <= 0)
	{
		$return = "fail";
	}
	else
	{
		$return = "success";
	}
}
//动物跑路
else if ($action == "runAwayAnimal")
{
	$animalId = $_POST['animalId'];
	$targetLiveId = $_POST['targetLiveId'];
	$sql = "update ".$tb_animal_profile." set liveId = ".$targetLiveId." where id = ".$animalId;
	$db->query($sql);
	if ($db->affectedRows() <= 0)
	{
		$return = "fail";
	}
	else
	{
		$return = "success";
	}
}
//指认通缉逃犯
else if ($action == "identifyWantedAnimal")
{
	$detectiveOwnerId = $_POST['detectiveOwnerId'];
	$suspectAnimalId = $_POST['suspectAnimalId'];
	$claimBones = $_POST['claimBones'];
	$sql = "select sum(wantedBones) as wantedBonesSum from ".$tb_battle_case." where winnerAnimalId = ".$suspectAnimalId." and timeToClose > ".$timeStamp;
	$result = $db->query($sql);
	if (($arr = $db->fetchArray($result)) && $arr['wantedBonesSum'] > 0)
	{
		$xmlContent .= "<wantedBonesSum><![CDATA[".$arr['wantedBonesSum']."]]></wantedBonesSum>\n";
		$sql = "select a.id as caseId, loserAnimalId, b.ownerId as loserOwnerId, caseBones, ownerDetectedFlag, otherDetectedFlag from ".$tb_battle_case." a inner join ".$tb_animal_profile." b on a.loserAnimalId = b.id where winnerAnimalId = ".$suspectAnimalId." and timeToClose > ".$timeStamp;
		$result = $db->query($sql);
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<battleCase>\n";
			$xmlContent .= "<caseId><![CDATA[".$arr['caseId']."]]></caseId>\n";
			$xmlContent .= "<loserAnimalId><![CDATA[".$arr['loserAnimalId']."]]></loserAnimalId>\n";
			$xmlContent .= "<loserOwnerId><![CDATA[".$arr['loserOwnerId']."]]></loserOwnerId>\n";
			$xmlContent .= "<caseBones><![CDATA[".$arr['caseBones']."]]></caseBones>\n";
			$xmlContent .= "<ownerDetectedFlag><![CDATA[".$arr['ownerDetectedFlag']."]]></ownerDetectedFlag>\n";
			$xmlContent .= "<otherDetectedFlag><![CDATA[".$arr['otherDetectedFlag']."]]></otherDetectedFlag>\n";
			$xmlContent .= "</battleCase>\n";
		}
		$sql = "update ".$tb_battle_case." set wantedBones = 0, timeToClose = 0 where winnerAnimalId = ".$suspectAnimalId;
		$result = $db->query($sql);	
		$return = "right";
	}
	else
	{
		//付出赔偿金
		$sql = "update ".$tb_user_own_object." set objectNum = objectNum - ".$claimBones." where userId = ".$detectiveOwnerId." and objectIndex = 0";
		$db->query($sql);
		//获得物品清单
		$sql = "select objectIndex, objectNum from ".$tb_user_own_object." where userId = ".$userId;
		$result = $db->query($sql);
		$xmlContent .= "<packageInfo>\n";
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<object>\n";
			$xmlContent .= "<userId><![CDATA[".$userId."]]></userId>\n";
			$xmlContent .= "<objectIndex><![CDATA[".$arr['objectIndex']."]]></objectIndex>\n";
			$xmlContent .= "<objectNum><![CDATA[".$arr['objectNum']."]]></objectNum>\n";
			$xmlContent .= "</object>\n";
		}
		$xmlContent .= "</packageInfo>\n";
		$return = "wrong";
	}
}
else if ($action == "startFight")
{
	$splitLeftAnimalId = split(',', $_POST['leftAnimalIds']);
	$splitRightAnimalId = split(',', $_POST['rightAnimalIds']);
	$leftPlayerId = $_POST['leftPlayerId'];
	$rightPlayerId = $_POST['rightPlayerId'];
	$fightOfTime = $_POST['fightOfTime'];
	$timeToCoolDownFight = $timeStamp + $fightOfTime;
	$xmlContent .= "<leftPlayerId><![CDATA[".$leftPlayerId."]]></leftPlayerId>\n";
	$xmlContent .= "<rightPlayerId><![CDATA[".$rightPlayerId."]]></rightPlayerId>\n";
	$xmlContent .= "<leftAnimalIds><![CDATA[".$_POST['leftAnimalIds']."]]></leftAnimalIds>\n";
	$xmlContent .= "<rightAnimalIds><![CDATA[".$_POST['rightAnimalIds']."]]></rightAnimalIds>\n";
	$sql = "update ".$tb_animal_profile." set timeToCoolDownFight = ".$timeToCoolDownFight." where id = ".$splitRightAnimalId[0]." and timeToCoolDownFight < ".$timeStamp;
	$result = $db->query($sql);
	if ($db->affectedRows() > 0)
	{
		//获得左侧玩家物品清单
		$sql = "select objectIndex, objectNum from ".$tb_user_own_object." where userId = ".$leftPlayerId;
		$result = $db->query($sql);
		$xmlContent .= "<packageInfo>\n";
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<object>\n";
			$xmlContent .= "<userId><![CDATA[".$leftPlayerId."]]></userId>\n";
			$xmlContent .= "<objectIndex><![CDATA[".$arr['objectIndex']."]]></objectIndex>\n";
			$xmlContent .= "<objectNum><![CDATA[".$arr['objectNum']."]]></objectNum>\n";
			$xmlContent .= "</object>\n";
		}
		$sql = "select currentOwnerPlayerId, originalOwnerPlayerId, b.name as playerName, c.level as level from ".$tb_heirloom." a inner join ".$tb_user_profile." b on a.originalownerPlayerId = b.id inner join ".$tb_animal_profile." c on b.id = c.ownerId where currentOwnerPlayerId = ".$leftPlayerId;
		$result = $db->query($sql);
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<heirloom>\n";
			$xmlContent .= "<originalOwnerPlayerId><![CDATA[".$arr['originalOwnerPlayerId']."]]></originalOwnerPlayerId>\n";
			$xmlContent .= "<originalPlayerName><![CDATA[".$arr['playerName']."]]></originalPlayerName>\n";
			$xmlContent .= "<currentOwnerPlayerId><![CDATA[".$arr['currentOwnerPlayerId']."]]></currentOwnerPlayerId>\n";
			$xmlContent .= "<heirloomLevel><![CDATA[".$arr['level']."]]></heirloomLevel>\n";
			$xmlContent .= "</heirloom>\n";
		}
		$xmlContent .= "</packageInfo>\n";
		//获得右侧玩家物品清单
		$sql = "select objectIndex, objectNum from ".$tb_user_own_object." where userId = ".$rightPlayerId;
		$result = $db->query($sql);
		$xmlContent .= "<packageInfo>\n";
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<object>\n";
			$xmlContent .= "<userId><![CDATA[".$rightPlayerId."]]></userId>\n";
			$xmlContent .= "<objectIndex><![CDATA[".$arr['objectIndex']."]]></objectIndex>\n";
			$xmlContent .= "<objectNum><![CDATA[".$arr['objectNum']."]]></objectNum>\n";
			$xmlContent .= "</object>\n";
		}
		$sql = "select currentOwnerPlayerId, originalOwnerPlayerId, b.name as playerName, c.level as level from ".$tb_heirloom." a inner join ".$tb_user_profile." b on a.originalownerPlayerId = b.id inner join ".$tb_animal_profile." c on b.id = c.ownerId where currentOwnerPlayerId = ".$rightPlayerId;
		$result = $db->query($sql);
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<heirloom>\n";
			$xmlContent .= "<originalOwnerPlayerId><![CDATA[".$arr['originalOwnerPlayerId']."]]></originalOwnerPlayerId>\n";
			$xmlContent .= "<originalPlayerName><![CDATA[".$arr['playerName']."]]></originalPlayerName>\n";
			$xmlContent .= "<currentOwnerPlayerId><![CDATA[".$arr['currentOwnerPlayerId']."]]></currentOwnerPlayerId>\n";
			$xmlContent .= "<heirloomLevel><![CDATA[".$arr['level']."]]></heirloomLevel>\n";
			$xmlContent .= "</heirloom>\n";
		}
		$xmlContent .= "</packageInfo>\n";
		//得到所有动物
		$allAnimalIds = $_POST['leftAnimalIds'].",".$_POST['rightAnimalIds'];
		$splitAnimalId = split(',', $allAnimalIds);
		for ($i = 0; $i < count($splitAnimalId); $i++)
		{
			$xmlContent .= $server->getAnimalXML($splitAnimalId[$i]);
		}
		$return = "success";
	}
	else
	{
		$return = "fail";
	}
}
else if ($action == "endFight")
{
	$leftPlayerId = $_POST['leftPlayerId'];
	$rightPlayerId = $_POST['rightPlayerId'];
	$splitLeftAnimalId = split(',', $_POST['leftAnimalIds']);
	$splitRightAnimalId = split(',', $_POST['rightAnimalIds']);
	$sql = "update ".$tb_animal_profile." set timeToCoolDownFight = 0 where id = ".$splitRightAnimalId[0];
	$xmlContent .= "<leftPlayerId><![CDATA[".$leftPlayerId."]]></leftPlayerId>\n";
	$xmlContent .= "<rightPlayerId><![CDATA[".$rightPlayerId."]]></rightPlayerId>\n";
	$result = $db->query($sql);
	$return = "success";
}
else if ($action == "fetchHeirloomBox")
{
	$targetUserId = $_POST['targetUserId'];
	$getBones = $_POST['getBones'];
	$leftBones = $_POST['leftBones'];
	if ($targetUserId == $userId)
	{
		$sql = "update ".$tb_user_profile." set heirloomBoxBoneleft = 0, heirloomBoxFetchTime = ".$timeStamp." where id = ".$userId;
		$result = $db->query($sql);
	}
	else
	{
		$sql = "update ".$tb_user_profile." set heirloomBoxBoneleft = ".$leftBones." where id = ".$targetUserId;
		$result = $db->query($sql);
	}
	$xmlContent .= "<targetUserId><![CDATA[".$targetUserId."]]></targetUserId>\n";
	$xmlContent .= "<getBones><![CDATA[".$getBones."]]></getBones>\n";
	$xmlContent .= "<leftBones><![CDATA[".$leftBones."]]></leftBones>\n";
	$return = "success";
}
else if ($action == "switchHome")
{
	$oldLiveId = $_POST['oldLiveId'];
	$newLiveId = $_POST['newLiveId'];
	$xmlContent .= "<oldLiveId><![CDATA[".$oldLiveId."]]></oldLiveId>\n";
	$xmlContent .= "<newLiveId><![CDATA[".$newLiveId."]]></newLiveId>\n";

	$sql = "select name, renrenId, homeIndex, heirloomBoxBoneLeft, heirloomBoxFetchTime, heirloomLevelCount, heirloomLevelRank from ".$tb_user_profile." where id = ".$newLiveId;
	$result = $db->query($sql);
	if ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<heirloomIsSteal><![CDATA[".$arr['heirloomIsSteal']."]]></heirloomIsSteal>\n";
		$xmlContent .= "<newLiveUserName><![CDATA[".$arr['name']."]]></newLiveUserName>\n";
		$xmlContent .= "<newLiveRenrenId><![CDATA[".$arr['renrenId']."]]></newLiveRenrenId>\n";
		$xmlContent .= "<newLiveHomeIndex><![CDATA[".$arr['homeIndex']."]]></newLiveHomeIndex>\n";
		$xmlContent .= "<heirloomBoxBoneLeft><![CDATA[".$arr['heirloomBoxBoneLeft']."]]></heirloomBoxBoneLeft>\n";
		$xmlContent .= "<heirloomBoxFetchTime><![CDATA[".$arr['heirloomBoxFetchTime']."]]></heirloomBoxFetchTime>\n";
		$xmlContent .= "<heirloomLevelCount><![CDATA[".$arr['heirloomLevelCount']."]]></heirloomLevelCount>\n";
		$xmlContent .= "<heirloomLevelRank><![CDATA[".$arr['heirloomLevelRank']."]]></heirloomLevelRank>\n";
		//读取用户装备荣誉勋章
		$sql = "select equipId, equipLevel from ".$tb_player_honour." where playerId = ".$newLiveId;
		$result = $db->query($sql);
		$playerEquipScore = 0;
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<honourInfo>";
			$xmlContent .= "<equipsId><![CDATA[".$arr['equipId']."]]></equipsId>\n";
			$xmlContent .= "<equipsLevel><![CDATA[".$arr['equipLevel']."]]></equipsLevel>\n";
			$xmlContent .= "</honourInfo>";
			$playerEquipScore += $arr['equipLevel'];
		}
		//读取用户装备排名
		$sql = "select count(*) as equipRank from (select sum(equipLevel) as equipScore from ".$tb_player_honour." group by playerId) t where equipScore > ".$playerEquipScore;
		$result = $db->query($sql);
		if ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<equipRank><![CDATA[".$arr['equipRank']."]]></equipRank>\n";
		}
		//读取传家骨信息
		$sql = "select currentOwnerPlayerId, originalOwnerPlayerId, b.id as playerId, b.name as playerName, c.level as level from ".$tb_heirloom." a inner join ".$tb_user_profile." b on a.originalownerPlayerId = b.id inner join ".$tb_animal_profile." c on b.id = c.ownerId where currentOwnerPlayerId = ".$newLiveId;
		$result = $db->query($sql);
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<heirloom>\n";
			$xmlContent .= "<originalOwnerPlayerId><![CDATA[".$arr['originalOwnerPlayerId']."]]></originalOwnerPlayerId>\n";
			$xmlContent .= "<originalPlayerName><![CDATA[".$arr['playerName']."]]></originalPlayerName>\n";
			$xmlContent .= "<currentOwnerPlayerId><![CDATA[".$arr['currentOwnerPlayerId']."]]></currentOwnerPlayerId>\n";
			$xmlContent .= "<heirloomLevel><![CDATA[".$arr['level']."]]></heirloomLevel>\n";
			$xmlContent .= "</heirloom>\n";
		}
		$sql = "select a.id as animalId, a.name as animalName, b.name as ownerName, animalIndex, experience, level, healthPointMax, healthPoint, vitalityPointMax, vitalityPoint, intelligence, speed, skillPoint, ownerId, liveId, timeToGetFree, hpSaveTime from ".$tb_animal_profile." a inner join ".$tb_user_profile." b on a.ownerId = b.id where liveId = ".$oldLiveId." or liveId = ".$newLiveId;
		$result = $db->query($sql);
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<animal>\n";
			$xmlContent .= "<animalId><![CDATA[".$arr['animalId']."]]></animalId>\n";
			$xmlContent .= "<animalName><![CDATA[".$arr['animalName']."]]></animalName>\n";
			$xmlContent .= "<ownerName><![CDATA[".$arr['ownerName']."]]></ownerName>\n";
			$xmlContent .= "<animalIndex><![CDATA[".$arr['animalIndex']."]]></animalIndex>\n";
			$xmlContent .= "<experience><![CDATA[".$arr['experience']."]]></experience>\n";
			$xmlContent .= "<level><![CDATA[".$arr['level']."]]></level>\n";
			$xmlContent .= "<healthPointMax><![CDATA[".$arr['healthPointMax']."]]></healthPointMax>\n";
			$xmlContent .= "<healthPoint><![CDATA[".$arr['healthPoint']."]]></healthPoint>\n";
			$xmlContent .= "<vitalityPointMax><![CDATA[".$arr['vitalityPointMax']."]]></vitalityPointMax>\n";
			$xmlContent .= "<vitalityPoint><![CDATA[".$arr['vitalityPoint']."]]></vitalityPoint>\n";
			$xmlContent .= "<intelligence><![CDATA[".$arr['intelligence']."]]></intelligence>\n";
			$xmlContent .= "<speed><![CDATA[".$arr['speed']."]]></speed>\n";
			$xmlContent .= "<skillPoint><![CDATA[".$arr['skillPoint']."]]></skillPoint>\n";
			$xmlContent .= "<ownerId><![CDATA[".$arr['ownerId']."]]></ownerId>\n";
			$xmlContent .= "<liveId><![CDATA[".$arr['liveId']."]]></liveId>\n";
			$xmlContent .= "<timeToGetFree><![CDATA[".$arr['timeToGetFree']."]]></timeToGetFree>\n";
			$xmlContent .= "<hpSaveTime><![CDATA[".$arr['hpSaveTime']."]]></hpSaveTime>\n";
			$sql2 = "select position, equipId, objectIndex, durability from ".$tb_animal_ware_equip." a inner join ".$tb_equip_profile." b on a.equipId = b.id where a.animalId = ".$arr['animalId'];
			$result2 = $db->query($sql2);
			while ($arr2 = $db->fetchArray($result2))
			{
				$xmlContent .= "<equip>\n";
				$xmlContent .= "<equipId><![CDATA[".$arr2['equipId']."]]></equipId>\n";
				$xmlContent .= "<position><![CDATA[".$arr2['position']."]]></position>\n";
				$xmlContent .= "<objectIndex><![CDATA[".$arr2['objectIndex']."]]></objectIndex>\n";
				$xmlContent .= "<durability><![CDATA[".$arr2['durability']."]]></durability>\n";
				$xmlContent .= "</equip>\n";
			}
			$xmlContent .= "</animal>\n";
		}	
		$sql = "select a.liveId as liveId, a.id as animalId, b.wantedBones as wantedBones, b.timeToClose as timeToClose from ".$tb_animal_profile." a inner join ".$tb_battle_case." b on a.id = b.winnerAnimalId where liveId = ".$newLiveId;
		$result = $db->query($sql);
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<arrestInfo>\n";
			$xmlContent .= "<wantedBones><![CDATA[".$arr['wantedBones']."]]></wantedBones>\n";
			$xmlContent .= "<timeToClose><![CDATA[".$arr['timeToClose']."]]></timeToClose>\n";
			$xmlContent .= "<animalId><![CDATA[".$arr['animalId']."]]></animalId>\n";
			$xmlContent .= "</arrestInfo>\n";
		}
		$return = "success";
	}
	else
	{
		$return = "Id not exist";
	}
}
else if ($action == "balanceSkillPoint")
{
	$animalId = $_POST['animalId'];
	$addSkillPoint = $_POST['addSkillPoint'];
	$minusSkillPoint = -$addSkillPoint;
	$addHealthPoint = $_POST['addHealthPoint'];
	$addVitalityPoint = $_POST['addVitalityPoint'];
	$addIntelligence = $_POST['addIntelligence'];
	$addSpeed = $_POST['addSpeed'];

	$sql = "update ".$tb_animal_profile." set skillPoint = skillPoint + ".$addSkillPoint.", healthPointMax = healthPointMax + ".$addHealthPoint.", healthPoint = healthPoint + ".$addHealthPoint.", vitalityPointMax = vitalityPointMax + ".$addVitalityPoint.",  vitalityPoint = vitalityPoint + ".$addVitalityPoint.", intelligence = intelligence + ".$addIntelligence.", speed = speed + ".$addSpeed." where id = ".$animalId." and skillPoint >= ".$minusSkillPoint;
	$result = $db->query($sql);
	$xmlContent .= $server->getAnimalXML($animalId);
}
else if ($action == "accomplishTask")
{
	$taskType = $_POST['taskType'];
	$xmlContent .= "<taskType><![CDATA[".$taskType."]]></taskType>\n";
	if ($taskType == 0)
		$sql = "update ".$tb_user_profile." set accomplishedTaskId = accomplishedTaskId + 1 where id = ".$userId;
	else
		$sql = "update ".$tb_user_profile." set accomplishedFestivalTaskId = accomplishedFestivalTaskId + 1 where id = ".$userId;
	$result = $db->query($sql);
	if ($db->affectedRows() > 0)
	{
		$return = "success";
	}
	else
	{
		$return = "fail";
	}
}
//穿上装备
else if ($action == "getOnEquip")
{
	$animalId = $_POST['animalId'];
	$position = $_POST['position'];
	$objectIndex = $_POST['objectIndex'];
	$durability = $_POST['durability'];
	$sql = "insert into ".$tb_equip_profile." (objectIndex, durability) VALUES (".$objectIndex.", ".$durability.")";
	$result = $db->query($sql);
	$equipId = $db->lastInsertId();
	$sql = "update ".$tb_animal_ware_equip." set equipId = ".$equipId." where animalId = ".$animalId." and position = ".$position;
	$result = $db->query($sql);
	if ($db->affectedRows() <= 0)
	{
		$sql = "insert into ".$tb_animal_ware_equip." (animalId, position, equipId) VALUES (".$animalId.", ".$position.", ".$equipId.")";
		$debug = $sql;
		$result = $db->query($sql);
	}
	$xmlContent .= $server->getAnimalXML($animalId);
	$return = "success";
}
//脱下装备
else if ($action == "getOffEquip")
{
	$animalId = $_POST['animalId'];
	$position = $_POST['position'];
	$equipId = $_POST['equipId'];
	$objectIndex = $_POST['objectIndex'];
	$durability = $_POST['durability'];
	$sql = "delete from ".$tb_equip_profile." where id = ".$equipId;
	$result = $db->query($sql);
	$sql = "delete from ".$tb_animal_ware_equip." where animalId = ".$animalId." and position = ".$position;
	$result = $db->query($sql);
	$xmlContent .= $server->getAnimalXML($animalId);
	$return = "success";
}


$hpBalance = $_POST['hpBalance'];
if ($hpBalance != "")
{
	$xmlContent .= $server->balanceHP($userId, $hpBalance, $timeStamp);
}

$boneBalance = $_POST['boneBalance'];
if ($boneBalance != "")
{
	$xmlContent .= $server->balanceBone($userId, $boneBalance);
}

$expBalance = $_POST['expBalance'];
if ($expBalance != "")
{
	$xmlContent .= $server->balanceExp($userId, $expBalance);
}

$honourBalance = $_POST['honourBalance'];
if ($honourBalance != "")
{
	$xmlContent .= $server->balanceHonour($userId,$honourBalance);
}

$heirloomBalance = $_POST['heirloomBalance'];
if ($heirloomBalance != "")
{
	$xmlContent .= $server->balanceHeirloom($userId, $heirloomBalance);
}

}

//header("Content-Type: text/xml");
$txt = "";
$txt .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$txt .= "<values>\n";
$txt .= "<debug><![CDATA[".$debug."]]></debug>\n";
$txt .= "<debug2><![CDATA[".$debug2."]]></debug2>\n";
$txt .= "<action><![CDATA[".$action."]]></action>\n";
$txt .= "<result><![CDATA[".$return."]]></result>\n";
$txt .= "<timeStamp><![CDATA[".$timeStamp."]]></timeStamp>\n";
$txt .= $xmlContent;
$txt .= "</values>";

$db->close();

echo $txt;

?>
